package com.uflower.order.controller;

import com.uflower.common.utils.R;
import com.uflower.order.entity.OrderEntity;
import com.uflower.order.service.OrderService;
import com.uflower.order.vo.OrderCenterOrderVo;
import com.uflower.order.vo.OrderConfirmVo;
import com.uflower.order.vo.OrderSubmitResultVo;
import com.uflower.order.vo.OrderSubmitVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.UUID;
@Api(value = "订单Controller",tags = "订单Controller")
@Controller
@RequestMapping("order/order")
public class OrderWebController {
    @Autowired
    OrderService orderService;

    /**
     * 购物车界面点击结算，进入订单确认页
     * @param model
     * @return
     */
    @ApiOperation(value = "购物车界面点击结算，进入订单确认页")
    @GetMapping("/toTrade")
    public String toTrade(Model model){

        OrderConfirmVo confirmVo = orderService.confirmOrder();

        model.addAttribute("orderConfirmData",confirmVo);
        return "confirm";
    }
    /**
     *  订单确认页确认完成，进入支付页面
     * @param submitVo
     * @return
     */
    @ApiOperation(value = "订单确认页确认完成，进入支付页面")
    @ApiImplicitParam(name = "submitVo",value = "前端传来的订单数据",dataType = "com.uflower.order.vo.OrderSubmitVo",required = true)
    @PostMapping("/submitOrder")
    public String submitOrder(OrderSubmitVo submitVo,Model model){

        OrderSubmitResultVo res = orderService.submitOrder(submitVo);
        System.out.println(submitVo+"  订单提交的数据");
        if(res.getCode() == 20000){
            //success
            model.addAttribute("res",res);
            return "waitForPay";
        }else{
            //false
            System.out.println(res.getCode()+"  ...订单提交失败错误码");
            return "redirect:http://localhost:10002/order/order/toTrade";
        }

    }

    /**
     * 取消订单(未支付时)
     * @return
     */
    @ApiOperation(value = "取消订单(未支付时)")
    @ApiImplicitParam(name = "orderSn",value = "订单号",dataType = "String",required = true,paramType = "path")
    @GetMapping("/cancelOrder/{orderSn}")
    @ResponseBody
    public R cancelOrder(@PathVariable("orderSn") String orderSn){
        orderService.closeOrder(orderSn);
        return R.ok();
    }
    /**
     * 通过订单号查询订单
     * @param orderSn 订单号
     * @return
     */
    @ApiOperation(value = "通过订单号查询订单")
    @ApiImplicitParam(name = "orderSn",value = "订单号",dataType = "String",required = true,paramType = "path")
    @GetMapping("/status/{orderSn}")
    @ResponseBody
    public R getOrderStatus(@PathVariable("orderSn") String orderSn){
        OrderEntity orderEntity = orderService.getOrderByOrderSn(orderSn);
        return R.ok().put("order",orderEntity);
    }


    /**
     *
     *订单中心页面展示--订单查询
     */

//    @Autowired
//    RabbitTemplate rabbitTemplate;
//    @GetMapping("/test/createOrder")
//    public String test(){
//        OrderEntity orderEntity = new OrderEntity();
//        orderEntity.setOrderSn(UUID.randomUUID().toString());
//        orderEntity.setModifyTime(new Date());
//        rabbitTemplate.convertAndSend("order-event-exchange","order.create.order",orderEntity);
//        return "ok";
//    }

    /**
     *
     *订单中心页面展示--订单查询(筛选）
     */
    //    CREATE_NEW(0,"待付款"),
    //    PAYED(1,"已付款"),
    //    SENDEND(2,"已发货"),
    //    RECIEVED(3,"已完成"),
    //    CANCLED(4,"已取消"),
    //    SERVICING(5,"售后中"),
    //    SERVICED(6,"售后完成");
    @ApiOperation(value = "订单中心页面展示--订单查询(筛选) 用户"
            ,notes = "-1:所有订单\n 0:待付款\n 1:已付款未发货\n  2:已发货未收货\n 3:已完成\n 4:已取消\n"  )
    @ApiImplicitParam(name = "status",value = "订单状态",dataType = "Integer",required = true,paramType = "path")
    @GetMapping("/orderCenter/{status}")
    @ResponseBody
    public R showOrderCenter(@PathVariable("status") Integer status){
        List<OrderCenterOrderVo> orderVos = orderService.queryCurrentUserOrders(status);
        return R.ok().put("orders",orderVos);
    }
}
